package com.housing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.zmetaport.common.idempotent.annotation.RepeatSubmit;
import com.zmetaport.common.log.annotation.Log;
import com.zmetaport.common.web.core.BaseController;
import com.zmetaport.common.mybatis.core.page.PageQuery;
import com.zmetaport.common.core.domain.R;
import com.zmetaport.common.core.validate.AddGroup;
import com.zmetaport.common.core.validate.EditGroup;
import com.zmetaport.common.log.enums.BusinessType;
import com.zmetaport.common.excel.utils.ExcelUtil;
import com.housing.domain.vo.DictTypeVo;
import com.housing.domain.bo.DictTypeBo;
import com.housing.service.IDictTypeService;
import com.zmetaport.common.mybatis.core.page.TableDataInfo;

/**
 * 字典类型
 *
 * @author chensm
 * @date 2024-12-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/housing/type")
public class DictTypeController extends BaseController {

    private final IDictTypeService dictTypeService;

    /**
     * 查询字典类型列表
     */
    //@SaCheckPermission("housing:type:list")
    @GetMapping("/list")
    public TableDataInfo<DictTypeVo> list(DictTypeBo bo, PageQuery pageQuery) {
        return dictTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出字典类型列表
     */
    //@SaCheckPermission("housing:type:export")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DictTypeBo bo, HttpServletResponse response) {
        List<DictTypeVo> list = dictTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "字典类型", DictTypeVo.class, response);
    }

    /**
     * 获取字典类型详细信息
     *
     * @param dictId 主键
     */
    //@SaCheckPermission("housing:type:query")
    @GetMapping("/{dictId}")
    public R<DictTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dictId) {
        return R.ok(dictTypeService.queryById(dictId));
    }

    /**
     * 新增字典类型
     */
    //@SaCheckPermission("housing:type:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DictTypeBo bo) {
        return toAjax(dictTypeService.insertByBo(bo));
    }

    /**
     * 修改字典类型
     */
    //@SaCheckPermission("housing:type:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DictTypeBo bo) {
        return toAjax(dictTypeService.updateByBo(bo));
    }

    /**
     * 删除字典类型
     *
     * @param dictIds 主键串
     */
    //@SaCheckPermission("housing:type:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dictIds) {
        return toAjax(dictTypeService.deleteWithValidByIds(List.of(dictIds), true));
    }
}
